//
//  PersonSelectView.swift
//  App_Vision
//
//  Created by Flutter on 2021/8/6.
//

import UIKit
import UICore
import Center

protocol PersonSelectViewDelegate {
    func personSelectView(_ personSelectView: PersonSelectView, didSelectItemAt index: Int)
}

class PersonSelectView: View {
    
    var dataSource:[Person] = [] {
        didSet {
            collectView.reloadData()
        }
    }
    
    var delegate: PersonSelectViewDelegate?
    
    
    lazy var collectView: UICollectionView = {
        
        let flowLayout = PersonSelectViewFlowLayout()

        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: (bounds.width - 300)/4, bottom: 0, right: (bounds.width - 300)/4)
        flowLayout.minimumLineSpacing = (bounds.width - 300)/4
        
        flowLayout.itemSize = CGSize(width: 100,height: bounds.height)
        
        flowLayout.scrollDirection = .horizontal
        
        flowLayout.isZoom = true
        let collectView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
        collectView.backgroundColor = .clear
        collectView.delegate = self
        collectView.dataSource = self
        collectView.showsHorizontalScrollIndicator = false

        collectView.register(PersonCell.self, forCellWithReuseIdentifier: PersonCell.cellID)
//        collectView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never

        return collectView
        
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

private extension PersonSelectView {
    
    func setupUI(){
        
        collectView.leaf.add(to: self).snp.makeConstraints {
            $0.edges.equalToSuperview()
        }
        
        
    }
    
}

// MARK: - UIScrollViewDelegate

extension PersonSelectView: UIScrollViewDelegate {
    
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let perWidth = bounds.width / 3 - 10
        let row = CGFloat(lroundf(Float(scrollView.contentOffset.x / perWidth)))
        scrollView.setContentOffset(CGPoint(x: row * perWidth, y: 0), animated: true)
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if decelerate == false {
            let perWidth = bounds.width / 3 - 10
            let row = CGFloat(lroundf(Float(scrollView.contentOffset.x / perWidth)))
            scrollView.setContentOffset(CGPoint(x: row * perWidth, y: 0), animated: true)
        }
    }
    
    
}

// MARK: - UICollectionViewDelegate

extension PersonSelectView: UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let row = indexPath.row
        guard row > 0 && row < dataSource.count + 2 else {
            return
        }
        let perWidth = bounds.width / 3 - 10
        collectionView.setContentOffset(CGPoint(x: CGFloat(row - 1) * perWidth, y: 0), animated: true)
        self.delegate?.personSelectView(self, didSelectItemAt: row - 1)
        
    }
    
}

// MARK: - UICollectionViewDataSource

extension PersonSelectView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count + 3
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: PersonCell = collectionView.dequeueReusableCell(withReuseIdentifier: PersonCell.cellID, for: indexPath) as! PersonCell
        
        // 第一个和最后一个 要 hidden 隐身
        if indexPath.row == 0 || indexPath.row == (dataSource.count + 2) {
            cell.style = .Empty
        }
        // 添加 Person
        else if indexPath.row == (dataSource.count + 1) {
            cell.style = .Add
        } else {
            cell.style = .Content
            cell.person = dataSource[indexPath.row - 1]
        }
        return cell
    }
    
    
}
